﻿// SSL.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <WinSock2.h>  
#include <stdio.h>
#include "fileio.h"
#include "iodgram.h"
#include "ioutil.h"
#include "asyndns.h"
#include "sslwrap.h"
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"..\\Debug\\IOCP.lib")

char strText[] = "GET / HTTP/1.1\r\n"
"Host: www.baidu.com\r\n"
"Accept : */*\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"User-Agent: IoSelector/1.0\r\n"
"Connection: close\r\n\r\n";



void ssl_connect_proc(IOCPSSL* pSSL, void* pUserData)
{
	ssl_write(pSSL, strText, strlen(strText));
}

void ssl_accept_proc(IOCPSSL* pSSL1, IOCPSSL* pSSL2)
{

}

void ssl_write_proc(IOCPSSL* pSSL, void* pUserData)
{

}

void ssl_read_proc(IOCPSSL* pSSL, void* pUserData)
{
	char msg[1024] = { 0 };
	int len = 0;
	while(1)
	{
		memset(msg, 0, sizeof(msg));
		len = ssl_read(pSSL, msg, 1024 - 1);

		if (0 == len)
		{
			break;
		}

		printf(msg);
	}
}

void ssl_signal_proc(IOCPSSL* pSSL, DWORD dwEvent, DWORD dwError)
{

}

int main()
{
	iocp_init_env();

	HIOCPBASE hIocp = iobase_new();

	SOCKADDR_IN SockAddr = { 0 };
	int nAddrSize = sizeof(SOCKADDR_IN);
	SockAddr.sin_family = AF_INET;
	SockAddr.sin_port = htons(443);
	SockAddr.sin_addr.s_addr = inet_addr("14.215.177.39");

	SSL* pOrigh = SSL_new(SSL_CTX_new(SSLv23_method()));

	IOCPSSL* pSSL = ssl_new(hIocp, pOrigh);

	ssl_setcb(pSSL, ssl_read_proc, ssl_write_proc, ssl_signal_proc, NULL);

	ssl_connect(pSSL, &SockAddr, ssl_connect_proc);

	iobase_dispatch(hIocp);//消息循环


	iocp_uninit_env();

	printf("main: process is over!\n");
	getchar();

	return 0;
}